﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MiniCMS.Business;

namespace MiniCMS.Forms
{
    public partial class frmEditSearches : Form
    {
        public Action<string> UpdateString = null;

        public frmEditSearches(Action<string> action)
        {
            InitializeComponent();
            UpdateString = action;

            List<ListItem> operators = new List<ListItem>();
            operators.Add(new ListItem(" > ", "大于"));
            operators.Add(new ListItem(" < ", "小于"));
            operators.Add(new ListItem(" = ", "等于"));
            operators.Add(new ListItem(" <> ", "不等于"));
            operators.Add(new ListItem(" like ", "包含"));
            this.cbQCondition.ValueMember = "ID";
            this.cbQCondition.DisplayMember = "Name";
            this.cbQCondition.DataSource = operators;

            this.cbQColumns.SelectedIndex = 0;
        }

        private void frmEditSearches_Load(object sender, EventArgs e)
        {
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < lstQConditions.Items.Count; i++)
            {
                if (i>0)
                    sb.Append(" AND ");
                sb.Append(lstQConditions.Items[i].ToString() + "\r\n");
            }

            UpdateString(sb.ToString());
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string query = this.cbQColumns.SelectedItem.ToString() + this.cbQCondition.SelectedValue;
            if (this.cbQCondition.SelectedIndex == 4)
            {
                query += "'%" + this.tbQContent.Text + "%'";
            }
            else if (this.cbQColumns.SelectedIndex == 0)
            {
                query += this.tbQContent.Text;
            }
            else
                query += "'" + this.tbQContent.Text + "'";

            this.lstQConditions.Items.Add(query);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            this.lstQConditions.Items.Remove(this.lstQConditions.SelectedItem);
        }
    }
}
